﻿<extend name="Public:base" />
<block name="body">
    <style type="text/css">
       .input-group {
        /* position: relative; */
        display: table;
        border-collapse: separate;
        width: 200px;
    }   
    .col-sm-5{ position:relative; }
    .sy_ccs1{ position: absolute; left:30px; top:10px;}
  .tags{width: auto;min-width: 377px;}
    .choice-brand{
        height: 28px;
    }
</style>
<!--引入弹窗插件-->
<include file="Public:artdialog" />

<div class="main-content">
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){}
        </script>
        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
            <span style="color:#428bca">{$parent_nav.name}</span>
            </li>
            <li class="active">{$one_nav.name}</li>
        </ul><!-- .breadcrumb -->
    </div>
            <style type="text/css">
                .page-content {
                  padding: 0px 24px 24px;
                }
                
                .porlets-content_title {
                    height: 49px;
                    background: #b6d1f2;
                }
                .col-md-12 .icon-table{
                    color: #FFFFFF;
                    font-size: 18px;
                    font-weight: bolder;
                    line-height: 49px;
                }
            </style>
    <div class="page-content">

        <form action="{:U('Replenishment/ReplenishmentList')}" method="post" id="searchForm">
            <input  type="hidden" name="brand_ids" value="" id="brand_ids">
            <div class="row">
                <div class="col-md-12">
                    <div class="block-web">
                    <div class="row">
                    <div style="padding: 20px 0;background: #e9f0f9;height:100%;">
                         <div class="form-group" style="float: left;">
                            <div class="input-group col-sm-3">
                             <input class="form-control" id="title" type="text" name="title" placeholder="请输入搜索条件/自编码/条形码" size="15">
                         </div>
                     </div>
                     <div class="form-group" style="float: left;">
                        <div class="input-group col-sm-3">
                         <input class="form-control" id="goods_name" type="text" name="goods_name" placeholder="请输入商品名">
                     </div>
                 </div>

                 <div class="form-group" style="float: left;">
                        <div class="input-group col-sm-3">
                         <input class="form-control" id="chinese_goods_name" type="text" name="chinese_goods_name" placeholder="请输入中文商品名">
                     </div>
                 </div>
                 <div class="form-group" >
                    <div class="col-sm-12">
                        <div class="tags " id="brand_label" >
                           <button type="button" class="btn btn-sm btn-info choice-brand"  onclick="_esOpendialogmini('{:U('Replenishment/Replenishmentbrand')}','选择品牌','xuanzepinpai')"> 选择品牌</button>

                       </div> 

                   </div>
                   <button type="button" class="btn btn-sm btn-info"  onclick="load()"> 搜索</button>
                   <button type="button" class="btn btn-sm btn-info" onclick="sub_all()"> 查看所有</button>
                   <div style="clear:both;"></div>

                   <!-- <button type="button" class="btn btn-info" onclick="export_excel()"> 导出EXCEL</button> -->
               </div>
               
                  </div>
                   <div class="porlets-content">
                    <div class="porlets-content_title">
                        <div class="row" >
                            <div class="col-md-12 icon-table">{$one_nav.name}</div>
                        </div>
                    </div>
                <div class="table-responsive">
                    <div id="order_list_item_content">
                        <div class="item-list-bd" style="height:100px;line-height:100px;text-align:center;">
                            <img src="__PUBLIC__/admin/images/load3.gif" />
                            加载中...
                        </div>
                    </div>
                </div><!--/table-responsive-->
     <button type="button"  class="btn btn-sm " id="batch_purchase"  onclick="sub_bean_all()"  url="{:U('Order/sub_bean',array('order_id'=>$vo['order_id'],'nav_id'=>$navid))}" disabled="disabled"> 批量采购</button> 

        </div><!--/block-web-->
    </div><!--/col-md-12-->
</div>
</form>
</div>
</div>
<script type="text/javascript" src="__PUBLIC__/laydate/laydate.dev.js"></script>
<script type="text/javascript">
    var cookiename="brand_<?php echo UID; ?>";
    $(function(){
        //初始化
        RCWBrand();
        goSearch("#searchForm","order_list_item_content");


      $(document).keydown(function(event){ 
        if(event.keyCode==13){ 
        load(); 
        } 
        
     })
      
    });


    function load(){
      goSearch("#searchForm","order_list_item_content");
      $('#batch_purchase').attr("disabled",true).removeClass('btn-info');
    }

    function toParentChilder(id){
        $('#parent_id').val(id);
        goSearch("#searchForm","order_list_item_content");
    }

    function clock_load_dialog(){
     setTimeout(function(){
        var win=    art.dialog.opener;
        win.load();
        top.art.dialog().close();
    },800);
 }

function sub_all(){
    $('input[type=text]').val(['']);
    $('input[type=hidden]').val(['']);
    $('select').val('');
    //清空html
    $('#brand_label').children('span').remove();
    //删除cookie
    setCookie(cookiename,"",-10);
    load();
}

 function _es_openDilog90(url, title,id){ 
   art.dialog.open(url, { title: title, width: '1200px', height: '90%', opacity: 0.2, id: id, fixed: true, close:function(){load();}  });
}

function _esOpendialogmini(url, title,id){

    var mydialog=art.dialog.open(url,{title:title, width:'800px', height:'500px', opacity: 0.2, id: "add_edit_id",
      ok: function () { 
        this.close();
        RCWBrand();
        return false;
    },
    okVal:'确定',
    });  
}
function RCWBrand(){
   var ojson=[],lsjson=[],json=[],del_json=[]; 
   var htl="";
    //获取cookie值 
    var _list=getCookie(cookiename);
    if(_list!=""){
      ojson=JSON.parse(_list);
  } 
    //当前已经存在的商品信息 
    $("#brand_label").find('span').each(function(){
        var _json=$(this).attr("field");
        var _json="{"+_json+"}";
        var tmp =JSON.parse(_json);
        if(tmp != ""){
                  lsjson.push(tmp);  //当前特价商品信息
              }
          });
    
    if(ojson == null){ 
       ojson = [];
    }
    //过滤存在的品牌信息 
    for(var i=0;i<ojson.length;i++){
        var flg=false;
        for(var j=0;j<lsjson.length;j++){
            if(ojson[i].brand_id==lsjson[j].brand_id){
                flg=true;
                break;
            }
        }
        if(!flg){json.push(ojson[i]);}
    }
   
    //看是否有删除的品牌信息 
    for(var j=0;j<lsjson.length;j++){
       var flg=false;
       for(var i=0;i<ojson.length;i++){
        if(ojson[i].brand_id==lsjson[j].brand_id){
            flg=true;
            break;
        }
    }
    if(!flg){del_json.push(lsjson[j]);}
    }
 
    //删除不存在的标签
    if(del_json.length!=0){
         for(var i=0;i<del_json.length;i++){
          var _barnd = $('#brand_'+del_json[i].brand_id);
          _barnd.html('');
        }  
    }

    if(json==null)return;
     htl= "";
    for(var i=0;i<json.length;i++){
        htl +='<span class="tag" id="brand_'+json[i].brand_id+'" field=\'"brand_id":"'+json[i].brand_id+'","brand_name":"'+json[i].brand_name+'"\'>'+json[i].brand_name+'<button type="button" onclick="del_brand(this)" class="close">×</button></span>'; 
    }   
    if(htl!=""){
        $("#brand_label").append(htl); 
    } 
    //将brand_id 保存到form中
    brand_id_save_to_form();
}

function brand_id_save_to_form(){
   var _brand_ids ='';
   var _list=getCookie(cookiename);
   if(_list==""){
      $('#brand_ids').val(''); return;
  }
   ojson=JSON.parse(_list);
   if(ojson == null){
     ojson = {};
   }
  for(var i=0;i<ojson.length;i++){
     _brand_ids += (_brand_ids==""?'':',')+ ojson[i]['brand_id'];
 }
 $('#brand_ids').val(_brand_ids);

}
//点击删除brand标签
function del_brand(_this){
    //删除该条html
    var ojson=[],new_json = [];
    var _json = $(_this).parent('span').attr('field');
    $(_this).parent('span').remove();
   //删除cookie中的这条记录
      //获取该条记录
      var _json="{"+_json+"}";
      var tmp =JSON.parse(_json);
      var _list=getCookie(cookiename);
      if(_list!=""){
          ojson=JSON.parse(_list);
      } 
      for(var i=0;i<ojson.length;i++){
        var flag = false;
        if(ojson[i].brand_id == tmp.brand_id){
            flag = true;
        }
        if(!flag){new_json.push(ojson[i]);}
    }
     //重新保存cookie
     setCookie(cookiename,"",-10);
     if(new_json.length>0){
        var _db=JSON.stringify(new_json);
        setCookie(cookiename,_db,1);
    }
     //将brand_id 保存到form中 
     brand_id_save_to_form();
 }

    //写入cookie
    function setCookie(c_name, value, expiredays){
        if(expiredays < 0){
            localStorage.removeItem(c_name);
        }
       localStorage.setItem(c_name,value); //存储名字为name值为caibin的变量
   }
    //读取cookie   
    function getCookie(c_name){
        return  localStorage.getItem(c_name);
    }



//批量采购
function selallval(){
     var _list="";
   var _json =[];
     var flag = 1;
     $("#sample-table-2 input[type='checkbox']:checked").each(function(){
         // var flg=$(this).is(":checked");
         //if(!flg)return false;
         var arr = {};
         var number = $(this).parent('td').nextAll().children(".number").val();
         var GoodsPrice = $(this).parent('td').nextAll('.price').html();
         var _val=$(this).val();
         if(number >0 ){
             flag = 0;
            arr['GoodsSn'] = _val;
            arr['Quantity'] = number;
            arr['GoodsPrice'] = GoodsPrice; 
             _json.push(arr);
         }
     });
     if(flag == 1){
         art.dialog({title:'温馨提示',content: '<span>请填写需要采购数量,数量为0默认不采购!</span>', ok: function () {this.close();return false;},okval:'确定'});
        return '';
     }
     return _json;
} 


function sub_bean_all(){
     //获取需要删除信息
     var _val=selallval();
     if(_val==""){
        return false;
     }
/*     if(_val==""){
        art.dialog({title:'温馨提示',content: '<span>请勾选需要操作的记录!</span>', ok: function () {this.close();return false;},okval:'确定'});
        return false;
     }*/
     var htl = "";
     var sum = 0;
     var  datas = {};
     var _json = [];
     $.each(_val,function(i){
        if(_val[i].Quantity>0 && _val[i].GoodsPrice>0){
                 htl += "<tr><td>"+_val[i].GoodsSn+"</td><td>"+_val[i].Quantity+"</td><td>"+_val[i].GoodsPrice+"</td><td>"+(_val[i].Quantity*_val[i].GoodsPrice).toFixed(2)+"</td><td><a href='#'  onclick='del_htm(this)'>删除</a></td></tr>";
                datas['GoodsSn'] = _val[i].GoodsSn;
                datas['Quantity'] = _val[i].Quantity;
                 _json.push(datas);
                sum += _val[i].Quantity*_val[i].GoodsPrice;
        }
     })
    htl += "<tr><td  colspan='2'>小计：<span id='sum_price'>"+sum.toFixed(2)+"</span></td></tr>";
     $('#tab_lists').append(htl);
     var data = JSON.stringify(_json);
     var url = "{:U('Replenishment/goods_purchase_all')}";
     quantity_price2(url,data);
}


//提交前先计算所采购的数量价格
function quantity_price2(url,number){
    
    var dialog = art.dialog({
    title: '采购详情',
    content: document.getElementById("boxy"),
    ok: function(){
                var data =sub_tab_goods();
                if(data.length == 0){
                   art.dialog({title:'温馨提示',content: "请先选择要采购的商品", ok: function () {this.close(); return false;},okval:'确定'}); 
                }
                 _show_public_loading();
                     $.ajax({
                             'url': url,
                             'data': {'data':data},
                             // 'dataType': 'json',
                             'type': 'POST',
                             success: function (data) {
                                 if (data.state =="1") {  
                                    $('#money').html(data.money);
                                     dialog.close();
                                    art.dialog({title:'温馨提示',content: "采购成功", ok: function () {this.close();  load();return false;},okval:'确定'});
                                 }else{
                                     art.dialog({title:'温馨提示',content: data.msg, ok: function () {this.close(); return false;},okval:'确定'}); 
                                 }
                             }, error: function (err) { 
                              console.log(err);
                                 // art.dialog({title:'温馨提示',content: err.responseText, ok: function () {this.close();return false;},okval:'确定'});
                             },complete: function () {
                                _hide_public_loading();
                             }
                         });
                
    },
    cancelVal: '关闭',
    cancel:true,
    close:function(){
      load();
    }
});

/*
 var dialog = art.dialog();
              dialog.title('采购详情');
              dialog.lock(true);
              dialog.content(document.getElementById("boxy"));
              
              dialog.close(function(){
                alert(123);
              });
              $("#cancer").click(function (){
                             dialog.close();
                             load();
               });   
              $('#sub_tab').click(function() {
                var data =sub_tab_goods();
                
                 _show_public_loading();
                     $.ajax({
                             'url': url,
                             'data': {'data':data},
                           
                             'type': 'POST',
                             success: function (data) {
                                debugger;
                                 if (data.state =="1") {  
                                    $('#money').html(data.money);
                                     dialog.close();
                                    art.dialog({title:'温馨提示',content: "采购成功", ok: function () {this.close();  load();return false;},okval:'确定'});
                                 }else{
                                     art.dialog({title:'温馨提示',content: data.msg, ok: function () {this.close(); return false;},okval:'确定'}); 
                                 }
                             }, error: function (err) { 
                              console.log(err);
                     
                             },complete: function () {
                                _hide_public_loading();
                             }
                         });
              });*/
}



function del_htm(_this){
   $(_this).parent('td').parent('tr').remove();
   var price = $(_this).parent('td').parent('tr').children('td').eq(2).html();
   var number = $(_this).parent('td').parent('tr').children('td').eq(1).html();
   var sum_price = $('#sum_price').html()-(price*number);
    $('#sum_price').html(sum_price.toFixed(2));
}

//提交表单中的采购商品
function sub_tab_goods(){
  var arr2 = {};
  var _jsonstr =[];
  $('#tab_lists').children('tr').each(function() {
       var sn =  $(this).children('td').eq(0).text();
       var num=  $(this).children('td').eq(1).text();
       if(sn != "" && num != ""){
         arr2 ={'GoodsSn':sn,'Quantity':num};
        _jsonstr.push(arr2);
       }
  });
   var datas = JSON.stringify(_jsonstr);
   return datas;
}
</script>
</block>
